package com.siemens.industrial.web.mvc.controller.partner.user;

import com.siemens.industrial.model.partner.Device;
import com.siemens.industrial.model.partner.User;
import com.siemens.industrial.utils.GlobalResponseCode;
import com.siemens.industrial.utils.RestResponse;
import com.siemens.industrial.utils.SiemensException;
import com.siemens.industrial.web.mvc.service.AuthService;
import com.siemens.industrial.web.mvc.service.admin.TenantService;
import com.siemens.industrial.web.mvc.service.partner.PartnerDeviceService;
import com.siemens.industrial.web.mvc.service.partner.PartnerProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by qupen on 2016/11/8.
 */
@Controller
public class PartnerProductController {
    @Resource
    PartnerProductService partnerProductService;

    @Resource
    TenantService tenantService;

    @Resource
    AuthService authService;

    @Resource
    PartnerDeviceService partnerDeviceService;


    @RequestMapping(value = "/partner/{tenant}/product/devices", method = RequestMethod.GET)
    public String getDevicesPage(@PathVariable("tenant") String tenantKey, ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        modelMap.put("key", tenantKey);
        return "partner/user/devices";
    }

    @RequestMapping(value = "/partner/{tenant}/product/list", method = RequestMethod.GET)
    public String getProductPage(@PathVariable("tenant") String tenantKey, String deviceId, ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        modelMap.put("deviceId", deviceId);
        modelMap.put("key", tenantKey);
        return "partner/user/historyData";
    }

    @RequestMapping(value = "/partner/{tenant}/product/edit", method = RequestMethod.GET)
    public String getProductEditPage(@PathVariable("tenant") String tenantKey,
                                     String deviceId,
                                     String startTime,
                                     ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        Map<String, Object> data = partnerProductService.getOneProduct(tenantId, deviceId, startTime);
        modelMap.put("key", tenantKey);
        modelMap.put("data", data);
        return "partner/user/device_record";
    }

    @RequestMapping(value = "/partner/{tenant}/device/addDate", method = RequestMethod.GET)
    public String getAddDatePage(@PathVariable("tenant") String tenantKey,
                                 String deviceId,
                                 String uniqueId,
                                 String time,
                                 ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String, Object>> list = partnerProductService.findShiftByDeviceIdAndTime(tenantId, time, deviceId);
        modelMap.put("key", tenantKey);
        modelMap.put("shifts", list);
        modelMap.put("uniqueId", uniqueId);
        modelMap.put("time", time);
        modelMap.put("deviceId", deviceId);
        return "/partner/user/deviceAddData";
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/product/list", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getProductList(@PathVariable("tenant") String tenantKey,
                                              @RequestParam(name = "deviceId") String deviceId) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String, Object>> list = partnerProductService.getAllProduct(tenantId, deviceId);

        return RestResponse.create(GlobalResponseCode.SUCCESS)
                .putData(list)
                .build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/product/update", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getUpdateProduct(@PathVariable("tenant") String tenantKey,
                                                @RequestBody Map<String, Object> data) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        User user = (User) authService.currentUser();
        return partnerProductService.updateProduct(tenantId, data, user);
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/product/create", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> createProduct(@PathVariable("tenant") String tenantKey,
                                             @RequestBody ArrayList<Object> data) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        User user = (User) authService.currentUser();
        return partnerProductService.saveProduct(tenantId, data, user);
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/product/devices", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDevicesPage(@PathVariable("tenant") String tenantKey) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();

        List<Device> devices = partnerDeviceService.getAll(tenantId);

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(devices).build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/product/service", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findServicesByTime(@PathVariable("tenant") String tenantKey,
                                                  @RequestParam(name = "startTime") String startTime,
                                                  @RequestParam(name = "endTime") String endTime,
                                                  @RequestParam(name = "deviceId") String deviceId) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String, Object>> list = partnerProductService.findServicesByTime(tenantId, startTime, endTime, deviceId);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(list).build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/product/info", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findDeviceAndProduct(@PathVariable("tenant") String tenantKey,
                                                    @RequestParam(name = "time") Date time) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        if (time == null) {
            time = new Date();
        }
        List<String> listTime = partnerProductService.findAWeekTime(tenantId, time);
        List<Map<String, Object>> listInfo = partnerProductService.findDeviceAndProduct(tenantId, listTime.get(0), listTime.get(listTime.size() - 1));
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(listInfo).put("time", listTime).build();
    }
}
